package ru.ipo.dces.plugins.expressolympu2;/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import ru.ipo.dces.debug.ServerEmulator;
import ru.ipo.dces.utils.ResultUtils;
import java.util.Map;
import ru.ipo.problemsapi.ExtendedProblem;
import ru.ipo.dces.exceptions.GeneralRequestFailureException;

/**
 *
 * @author pushking
 */
public class ExpressOlympChecker implements ServerEmulator {
    String usercheck;
    String useranswer;
    String answertrue;
    int scores;
    
    @Override
    public Map<String, String> checkSolution(Map<String, String> solution, ExtendedProblem problem) throws GeneralRequestFailureException {

        useranswer = solution.get("useranswer");
        answertrue = problem.getString("right answer"); //добываем строку из задачи
        checkAnswer();

        return ResultUtils.newMap(
                "scores",
                ""+scores,
                "usercheck",
                usercheck
                );
    }

    private void checkAnswer() {
        scores = 0;
        usercheck = "";
        for (int i = 0; i < 5; i++) {
            if (useranswer.charAt(i) == answertrue.charAt(i)) {
                trueAnswer();
            } else if (useranswer.charAt(i) == '0') {
                skipAnswer();
            } else {
                falseAnswer();
            }
        }
    }

    private void trueAnswer() {
        scores += 2;
        usercheck += "+";
    }

    private void falseAnswer() {
        usercheck += "-";
    }

    private void skipAnswer() {
        scores++;
        usercheck += "0";
    }
}